<?php

namespace app\api\controller;

use app\api\service\OrderService;
use app\api\service\UserOrderService;
use app\ApiController;

    class UserOrderController extends ApiController
{
    /**
     * 用户订单
     */
    public function index()
    {
        $param =request()->all();

        $orderService = new OrderService();
        $res = $orderService->getUserOrderList($param, \Tinywan\Jwt\JwtToken::getCurrentId());
        return $this->success($res);
    }

    /**
     * 订单信息
     */
    public function detail()
    {
        $orderId =request()->input('id');

        $orderService = new OrderService();
        $res = $orderService->getOrderDetail($orderId, \Tinywan\Jwt\JwtToken::getCurrentId());
        return $this->success($res);
    }

    /**
     * 取消订单
     */
    public function cancel()
    {
        $orderId =request()->input('id');

        $orderService = new UserOrderService();
        $res = $orderService->orderCancel($orderId, \Tinywan\Jwt\JwtToken::getCurrentId(),\Tinywan\Jwt\JwtToken::getUser()['name']);
        return $this->success($res);
    }

    /**
     * 去支付
     */
    public function goPay()
    {
        $param = request()->post();
        $param['platform'] = isset(request()->header()['x-csrf-token']) ? request()->header()['x-csrf-token'] : '';

        $userOrderService = new UserOrderService();
        $res = $userOrderService->goPay($param, \Tinywan\Jwt\JwtToken::getCurrentId());
        return $this->success($res);
    }

    /**
     * 确认收货
     */
    public function received()
    {
        $orderId =request()->input('id');

        $userOrderService = new UserOrderService();
        $res = $userOrderService->doReceived($orderId, \Tinywan\Jwt\JwtToken::getCurrentId(), \Tinywan\Jwt\JwtToken::getUser()['name']);
        if ($res['code'] == 0) {
            $res['msg'] = '操作成功';
        }

        return $this->success($res);
    }

    /**
     * 评价
     */
    public function appraise()
    {
        if (request()->method() === 'POST') {

            $userOrderService = new UserOrderService();
            return json($userOrderService->doAppraise(request()->post(),  \Tinywan\Jwt\JwtToken::getUser()));
        }
        $orderId =request()->input('order_id');
        $orderDetailId =request()->input('order_detail_id');

        $userOrderService = new UserOrderService();
        $res = $userOrderService->getGoodsComments($orderId, $orderDetailId, \Tinywan\Jwt\JwtToken::getCurrentId());
        return $this->success($res);
    }

    /**
     * 物流信息
     */
    public function express()
    {
        $id =request()->input('id');

        $userOrderService = new UserOrderService();
        $res = $userOrderService->getExpressInfo($id, \Tinywan\Jwt\JwtToken::getCurrentId());
        return $this->success($res);
    }

}
